# PA Panel Replication
# 06-28-2024

## NOTE: CHANGE YOUR WORKING DIRECTORY to match the file path for the downloaded data
#setwd("~/Documents/GitHub/pa-panel")

# All necessary data; be sure to use readRDS() and NOT load()
dta.m <- readRDS("w13_data_06282024.rds")
ts.matched.06212021.b <- readRDS("mysm_prod_06272024.Rdata")
voterfile <- readRDS("mysm_prod_06272024.Rdata")
dta22 <- readRDS("cleaned_dta22.Rdata")
civiqs_id <- read.csv(file="merged-targets12-06282024.csv",header=T)

# Packages
library(dplyr)
library(xtable)
library(ipumsr)
####

# Figure 2 (barplot) ####

t1 <- table(dta.m$LAST.THREE.GEN.0321)/sum(table(dta.m$LAST.THREE.GEN.0321))
t2 <- table(ts.matched.06212021.b$LAST.THREE.GEN.0321)/sum(table(ts.matched.06212021.b$LAST.THREE.GEN.0321))

bp <- barplot(rbind(t1,t2),beside=T,ylab="Fraction",cex.lab=1.3,ylim=c(0,1),xlab="Total Turnout, 2016-2020",sub="Number of Gen. Elections")
text(y=.85,x=10.25,"Panelists",cex=1.5)
text(y=.225,x=5.75,"Voter File",cex=1.5)

# End Figure 2 ####


# Table 2 ####
# Run BEFORE Table 9
# 20,000 random sample from voter file
##

statewide <- (! dta.m$response_date.W1 %in% c(NA) |
                ! dta.m$response_date.W2 %in% c(NA) |
                ! dta.m$response_date.W3 %in% c(NA) |
                ! dta.m$response_date.W5 %in% c(NA) |
                ! dta.m$response_date.W6 %in% c(NA) |
                ! dta.m$response_date.W8 %in% c(NA) |
                ! dta.m$response_date.W11 %in% c(NA) |
                ! dta.m$response_date.W12 %in% c(NA)
)

statewide.id <- dta.m$civiqs_id[statewide]

# full PA panel --- dta.m
# PA panel with >1 statewide survey --- pa_panel
# 610 validated representative sample with >1 statewide survey --- pa_panel_val_rep
# 2206 validated unrepresentative sample, (610 + others) --- pa_panel_val_unrep

## 
pa_panel <- dta.m[dta.m$civiqs_id %in% statewide.id,]

pa_panel_val_unrep <- subset(dta.m, subset = !is.na(dta.m$vb.vf_g2020.TS.0321))
pa_panel_val_rep <- subset(pa_panel, subset = !is.na(pa_panel$vb.vf_g2020.TS.0321))

#### Voterfile benchmarking - descriptive statistics (validated PA panel (610) vs voter file sample (20000)) ####

# PA panel
pa_panel_val_rep$female <- NA
pa_panel_val_rep$female[pa_panel_val_rep$vb.voterbase_gender.TS.0321 == "Female"] <- 1
pa_panel_val_rep$female[pa_panel_val_rep$vb.voterbase_gender.TS.0321 == "Male"] <- 0

prop_female <- c(prop.table(table(voterfile$female))[2], prop.table(table(pa_panel_val_rep$female))[2])

## Age

# Voter file
round(mean(voterfile$DOB_YEAR, na.rm = T), digits = 0)
# 1970

# PA panel
round(mean(pa_panel_val_rep$vb.voterbase_dob.TS.0321_YEAR, na.rm = T), digits = 0)
# 1968
##

mean_birthyear <- c("1970", "1968")
prop_18to29 <- c(prop.table(table(voterfile$vb.voterbase_age < 30))[2], prop.table(table(pa_panel_val_rep$vb.voterbase_age.TS.0321 < 30))[2])
prop_over65 <- c(prop.table(table(voterfile$vb.voterbase_age > 64))[2], prop.table(table(pa_panel_val_rep$vb.voterbase_age.TS.0321 > 64))[2])

## Race - percent White, Black, Asian, Hispanic, Native, and "uncoded"

vf_white <- table(voterfile$vb.voterbase_race)[4]/(20000)
vf_black <- table(voterfile$vb.voterbase_race)[2]/(20000)
vf_asian <- table(voterfile$vb.voterbase_race)[3]/(20000)
vf_hisp <- table(voterfile$vb.voterbase_race)[5]/(20000)
vf_native <- table(voterfile$vb.voterbase_race)[6]/(20000)
vf_uncoded <- table(voterfile$vb.voterbase_race)[7]/(2000)

# PA panel

pa_white <- table(pa_panel_val_rep$vb.voterbase_race.TS.0321)[4]/(610)
pa_black <- table(pa_panel_val_rep$vb.voterbase_race.TS.0321)[2]/(610)
pa_asian <- table(pa_panel_val_rep$vb.voterbase_race.TS.0321)[3]/(610)
pa_hisp <- table(pa_panel_val_rep$vb.voterbase_race.TS.0321)[5]/(610)
pa_native <- 0
pa_uncoded <- table(pa_panel_val_rep$vb.voterbase_race.TS.0321)[6]/(610)

prop_white <- c(vf_white, pa_white) 
prop_black <- c(vf_black, pa_black) 
prop_asian <- c(vf_asian, pa_asian) 
prop_hisp <- c(vf_hisp, pa_hisp) 
prop_native <- c(vf_native, pa_native)
prop_uncoded_race <- c(vf_uncoded, pa_uncoded)

# Urbanicity

rural_VF = (table(voterfile$ts.tsmart_urbanicity)[2] + table(voterfile$ts.tsmart_urbanicity)[3])/(20000)
suburban_VF = (table(voterfile$ts.tsmart_urbanicity)[4] + table(voterfile$ts.tsmart_urbanicity)[5])/(20000)
urban_VF = (table(voterfile$ts.tsmart_urbanicity)[6] + table(voterfile$ts.tsmart_urbanicity)[7])/(20000)

rural_PA = (table(pa_panel_val_rep$ts.tsmart_urbanicity.TS.0321)[2] + table(pa_panel_val_rep$ts.tsmart_urbanicity.TS.0321)[3])/(610)
suburban_PA = (table(pa_panel_val_rep$ts.tsmart_urbanicity.TS.0321)[4] + table(pa_panel_val_rep$ts.tsmart_urbanicity.TS.0321)[5])/(610)
urban_PA = (table(pa_panel_val_rep$ts.tsmart_urbanicity.TS.0321)[6] + table(pa_panel_val_rep$ts.tsmart_urbanicity.TS.0321)[7])/(610)

prop_rural <- c(rural_VF, rural_PA)
prop_suburban <- c(suburban_VF, suburban_PA)
prop_urban <- c(urban_VF, urban_PA)

# Proportion married
prop_married <- c(prop.table(table(voterfile$vb.voterbase_marital_status))[2], prop.table(table(pa_panel_val_rep$vb.voterbase_marital_status.TS.0321))[2])

## Party registration

vf_prop_reg_republican <- table(voterfile$vb.vf_party)[8]/20000
vf_prop_reg_democrat <- table(voterfile$vb.vf_party)[2]/20000
vf_prop_reg_other <- (20000 - table(voterfile$vb.vf_party)[2] - table(voterfile$vb.vf_party)[8])/20000

# PA panel

pa_prop_reg_republican <- table(pa_panel_val_rep$vb.vf_party.TS.0321)[6]/610
pa_prop_reg_democrat <- table(pa_panel_val_rep$vb.vf_party.TS.0321)[2]/610
pa_prop_reg_other <- (610 - table(pa_panel_val_rep$vb.vf_party.TS.0321)[2] - table(pa_panel_val_rep$vb.vf_party.TS.0321)[6])/610

prop_reg_repubican <- c(vf_prop_reg_republican, pa_prop_reg_republican)
prop_reg_democrat <- c(vf_prop_reg_democrat, pa_prop_reg_democrat)
prop_reg_other <- c(vf_prop_reg_other, pa_prop_reg_other)

## 2020 Presidential primary turnout 

vf_p2020_prop_vote <- (20000 - table(voterfile$vb.vf_p2020)[1]) / 20000
pa_p2020_prop_vote <- (610 - table(pa_panel_val_rep$vb.vf_p2020.TS.0321)[1]) / 610

prop_vote_p2020 <- c(vf_p2020_prop_vote, pa_p2020_prop_vote)

sample_totals <- c(nrow(voterfile), nrow(pa_panel_val_rep))

desc_stats <- rbind(prop_female, 
                    mean_birthyear, 
                    prop_18to29,
                    prop_over65,
                    prop_white, 
                    prop_black, 
                    prop_hisp, 
                    prop_asian, 
                    prop_native, 
                    prop_married,
                    prop_rural,
                    prop_suburban,
                    prop_urban,
                    prop_reg_democrat,
                    prop_reg_repubican,
                    prop_reg_other,
                    prop_vote_p2020,
                    sample_totals)

colnames(desc_stats) <- c("Voter file sample", "PA panel")
rownames(desc_stats) <- c("Proportion female", "Mean birth year",
                          "Proportion 18-29", "Proportion 65+", "Proportion White",
                          "Proportion Black", "Proportion Hispanic", "Proportion Asian",
                          "Proportion native", "Proportion married", "Proportion rural",
                          "Proportion suburban", "Proportion urban", "Proportion registered Democrat",
                          "Proportion registered Republican", "Proportion registered other",
                          "Proportion voted in 2020 primary", "N")

xtable(desc_stats, type = "latex")

# End Table 2 ####

# Table 3 ####

## NOTE: CPS files will need download instructions, will not be included in public files
# using same statewide and statewide.id objects as Table 2,
# so be sure to run Table 2 BEFORE running Table 3

dta.m2 <- dta.m[dta.m$civiqs_id %in% statewide.id,]

txt <- "cps_00006.xml"

ddi <- read_ipums_ddi(txt)
data <- read_ipums_micro(ddi)

#### code family income

data$FAMINCNUM <- NA
data$FAMINCNUM[data$FAMINC==100] <- 2.5
data$FAMINCNUM[data$FAMINC==210] <- 6.25
data$FAMINCNUM[data$FAMINC==300] <- 8.75
data$FAMINCNUM[data$FAMINC==430] <- 11.25
data$FAMINCNUM[data$FAMINC==470] <- 13.75
data$FAMINCNUM[data$FAMINC==500] <- 17.5
data$FAMINCNUM[data$FAMINC==600] <- 22.5
data$FAMINCNUM[data$FAMINC==710] <- 27.5
data$FAMINCNUM[data$FAMINC==720] <- 32.5
data$FAMINCNUM[data$FAMINC==730] <- 37.5
data$FAMINCNUM[data$FAMINC==740] <- 45
data$FAMINCNUM[data$FAMINC==820] <- 55
data$FAMINCNUM[data$FAMINC==830] <- 67.5
data$FAMINCNUM[data$FAMINC==840] <- 100
data$FAMINCNUM[data$FAMINC==841] <- 87.5
data$FAMINCNUM[data$FAMINC==842] <- 125
data$FAMINCNUM[data$FAMINC==843] <- 200

## education levels

data$HASBA <- NA
data$HASBA[data$EDUC %in% c(111,123,124,125)] <- 1
data$HASBA[data$EDUC %in% c(1,2,10,20,30,40,50,60,71,73,81,91,92)] <- 0

data$HASHS <- NA
data$HASHS[data$EDUC %in% c(73,81,91,92,111,123,124,125)] <- 1
data$HASHS[data$EDUC %in% c(1,2,10,20,30,40,50,60,71)] <- 0

## race

data$BLACK <- 1*(data$RACE==200)
data$WHITE <- 1*(data$RACE==100)
data$ASIANAM <- 1*(data$RACE==651)
data$HISPANIC <- 1*(! data$HISPAN==0)

data$RACEREC <- NA
data$RACEREC[data$ASIANAM==1] <- "Asian Am."
data$RACEREC[data$BLACK==1] <- "Black"
data$RACEREC[data$WHITE==1] <- "White"
data$RACEREC[data$HISPANIC==1] <- "Latino"

### reduce to registered voters
dta.r1 <- data[(data$VOTED==2 & ! data$VOTED %in% c("",NA)) |
                 (data$VOREG==2 & ! data$VOREG %in% c("",NA)),]
dta.r <- dta.r1[dta.r1$STATEFIP==42,]

rmat <- matrix(NA,6,4)

rmat[1,1] <- weighted.mean(dta.r$FAMINCNUM[dta.r$ASIANAM==1],
                           w=dta.r$VOSUPPWT[dta.r$ASIANAM==1],
                           na.rm=T)
rmat[1,2] <- weighted.mean(dta.r$FAMINCNUM[dta.r$BLACK==1],na.rm=T,
                           w=dta.r$VOSUPPWT[dta.r$BLACK==1])
rmat[1,3] <- weighted.mean(dta.r$FAMINCNUM[dta.r$HISPANIC==1],
                           w=dta.r$VOSUPPWT[dta.r$HISPANIC==1],
                           na.rm=T)
rmat[1,4] <- weighted.mean(dta.r$FAMINCNUM[dta.r$WHITE==1],
                           w=dta.r$VOSUPPWT[dta.r$WHITE==1], na.rm=T)

rmat[2,1] <- mean(dta.m2$INCOME[dta.m2$ASIAN==1],na.rm=T)
rmat[2,2] <- mean(dta.m2$INCOME[dta.m2$BLACK==1],na.rm=T)
rmat[2,3] <- mean(dta.m2$INCOME[dta.m2$HISP==1],na.rm=T)
rmat[2,4] <-mean(dta.m2$INCOME[dta.m2$WHITE==1],na.rm=T)

rmat[3,1] <- sd(dta.r$FAMINCNUM[dta.r$ASIANAM==1],na.rm=T)
rmat[3,2] <- sd(dta.r$FAMINCNUM[dta.r$BLACK==1],na.rm=T)
rmat[3,3] <- sd(dta.r$FAMINCNUM[dta.r$HISPANIC==1],na.rm=T)
rmat[3,4] <- sd(dta.r$FAMINCNUM[dta.r$WHITE==1],na.rm=T)

rmat[4,1] <- sd(dta.m2$INCOME[dta.m2$ASIAN==1],na.rm=T)
rmat[4,2] <- sd(dta.m2$INCOME[dta.m2$BLACK==1],na.rm=T)
rmat[4,3] <- sd(dta.m2$INCOME[dta.m2$HISP==1],na.rm=T)
rmat[4,4] <- sd(dta.m2$INCOME[dta.m2$WHITE==1],na.rm=T)

rmat[5,1] <- sum(dta.r$ASIANAM)
rmat[5,2] <- sum(dta.r$BLACK)
rmat[5,3] <- sum(dta.r$HISPANIC)
rmat[5,4] <- sum(dta.r$WHITE)

# !pa_panel$INCOME %in% c(NA) ## FROM GALL

panel_income <- pa_panel %>% filter(
  !INCOME %in% c(NA)
)

rmat[6,1] <- sum(panel_income$ASIAN)
rmat[6,2] <- sum(panel_income$BLACK)
rmat[6,3] <- sum(panel_income$HISP)
rmat[6,4] <- sum(panel_income$WHITE)

rownames(rmat) <- c("CPS Mean","PA panel Mean","CPS SD","PA panel SD", "CPS N", "PA panel N")
colnames(rmat) <- c("Asian Am","Black","Latino","White")

xtable(rmat,digits=rep(0,5))

# End Table 3 ####


# Table 4 ####
## NOTE: CPS files downloadable via IPUMS website, not included in replication files
##

rmat2 <- matrix(NA,4,4)

rmat2[1,1] <- weighted.mean(dta.r$HASHS[dta.r$ASIANAM==1],
                            w=dta.r$VOSUPPWT[dta.r$ASIANAM==1],
                            na.rm=T)
rmat2[1,2] <- weighted.mean(dta.r$HASHS[dta.r$BLACK==1],na.rm=T,
                            w=dta.r$VOSUPPWT[dta.r$BLACK==1])
rmat2[1,3] <- weighted.mean(dta.r$HASHS[dta.r$HISPANIC==1],
                            w=dta.r$VOSUPPWT[dta.r$HISPANIC==1],
                            na.rm=T)
rmat2[1,4] <- weighted.mean(dta.r$HASHS[dta.r$WHITE==1],
                            w=dta.r$VOSUPPWT[dta.r$WHITE==1], na.rm=T)

rmat2[2,1] <- mean(dta.m2$HASHS[dta.m2$ASIAN==1],na.rm=T)
rmat2[2,2] <- mean(dta.m2$HASHS[dta.m2$BLACK==1],na.rm=T)
rmat2[2,3] <- mean(dta.m2$HASHS[dta.m2$HISP==1],na.rm=T)
rmat2[2,4] <-mean(dta.m2$HASHS[dta.m2$WHITE==1],na.rm=T)

rmat2[3,1] <- weighted.mean(dta.r$HASBA[dta.r$ASIANAM==1],
                            w=dta.r$VOSUPPWT[dta.r$ASIANAM==1],
                            na.rm=T)
rmat2[3,2] <- weighted.mean(dta.r$HASBA[dta.r$BLACK==1],na.rm=T,
                            w=dta.r$VOSUPPWT[dta.r$BLACK==1])
rmat2[3,3] <- weighted.mean(dta.r$HASBA[dta.r$HISPANIC==1],
                            w=dta.r$VOSUPPWT[dta.r$HISPANIC==1],
                            na.rm=T)
rmat2[3,4] <- weighted.mean(dta.r$HASBA[dta.r$WHITE==1],
                            w=dta.r$VOSUPPWT[dta.r$WHITE==1], na.rm=T)

rmat2[4,1] <- mean(dta.m2$HASBA[dta.m2$ASIAN==1],na.rm=T)
rmat2[4,2] <- mean(dta.m2$HASBA[dta.m2$BLACK==1],na.rm=T)
rmat2[4,3] <- mean(dta.m2$HASBA[dta.m2$HISP==1],na.rm=T)
rmat2[4,4] <-mean(dta.m2$HASBA[dta.m2$WHITE==1],na.rm=T)


rownames(rmat2) <- c("CPS HS Mean","PA panel HS Mean","CPS BA Mean","PA panel BA Mean")
colnames(rmat2) <- c("Asian Am","Black","Latino","White")

xtable(rmat2,digits=rep(2,5))

# End Table 4 ####


# Table 5 ####
##

t.naes <- table(dta22$TRUMPVBIDEN14B,dta22$TRUMPVBIDEN15B)
t.pa <- table(dta.m$TRUMPVBIDEN.W1,dta.m$TRUMPVBIDEN.W8)

table5 <- prop.table(t.pa)

rownames(table5) <- c("Biden Feb/Mar","Neither Feb/Mar","Trump Feb/Mar")
colnames(table5) <- c("Biden Oct","Neither Oct","Trump Oct")

xtable(
  table5,digits=c(0,3,3,3))

# End Table 5 ####


# Table 6 ####
# NOTE: Run Table 5 code BEFORE Table 6
##

table6 <- prop.table(t.naes)

rownames(table6) <- c("Biden Jan","Neither Jan","Trump Jan")
colnames(table6) <- c("Biden Oct","Neither Oct","Trump Oct")

xtable(
  table6,digits=c(0,3,3,3))

# End Table 6 ####


# Table 7 ####

vars <- c("DEMOCRAT","REPUBLICAN","TRUMPVBIDEN.ALL","AGE","BLACK","HISP","WHITE","FEMALE","EDYEARS")
rmat2 <- matrix(NA,length(vars),4)
for(i in 1:length(vars)){
  txt <- paste("tout <- t.test(dta.m$",vars[i],"[dta.m$PREVA.W11==1],",
               "dta.m$",vars[i],"[dta.m$PREVA.W11==0])",sep="")
  eval(parse(text=txt))
  rmat2[i,1] <- tout$estimate[1]
  rmat2[i,2] <- tout$estimate[2]
  rmat2[i,3] <- tout$p.value
  
  txt2 <- paste("hold <- dta.m$",vars[i],"[dta.m$PREVA.W11 %in% c(0,1)]",sep="")
  eval(parse(text=txt2))
  
  rmat2[i,4] <- length(na.omit(hold))
}
rownames(rmat2) <- c("Democrat", "Republican", "Trump (1) vs. Biden (-1)",
                     "Age", "Black", "Hispanic/Latino", "White", "Female",
                     "Education (years)")
colnames(rmat2) <- c("Mean Before", "Mean After", "P-Value", "Total N")

# library(xtable)
xtable(rmat2,digits=c(0,3,3,3,0))

# End Table 7 ####


#### APPENDIX ####

# Table 9 ####
## pa_panel & pa_panel_val_rep created in Table 2, run Table 2 BEFORE Table 9
##

june_20 <- pa_panel %>%
  filter(!is.na(response_date.W6))

int_female <- c(prop.table(table(pa_panel_val_rep$FEMALE))[2], 
                prop.table(table(june_20$FEMALE))[2],
                prop.table(table(pa_panel$FEMALE))[2])
int_age <- c(mean(na.omit(pa_panel_val_rep$AGE)), 
             mean(na.omit(june_20$AGE)),
             mean(na.omit(pa_panel$AGE)))
int_edyears <- c(mean(na.omit(pa_panel_val_rep$EDYEARS)), 
                 mean(na.omit(june_20$EDYEARS)),
                 mean(na.omit(pa_panel$EDYEARS)))
int_income <- c(mean(na.omit(pa_panel_val_rep$INCOME)), 
                mean(na.omit(june_20$INCOME)),
                mean(na.omit(pa_panel$INCOME)))
int_democrat <- c(prop.table(table(pa_panel_val_rep$DEMOCRAT))[2], 
                  prop.table(table(june_20$DEMOCRAT))[2],
                  prop.table(table(pa_panel$DEMOCRAT))[2])
int_republican <- c(prop.table(table(pa_panel_val_rep$REPUBLICAN))[2], 
                    prop.table(table(june_20$REPUBLICAN))[2],
                    prop.table(table(pa_panel$REPUBLICAN))[2])
int_total <- c(nrow(pa_panel_val_rep), nrow(june_20), nrow(pa_panel))

internal_comps <- rbind(int_female, 
                        int_age, 
                        int_edyears, 
                        int_income, 
                        int_democrat, 
                        int_republican,
                        int_total)

colnames(internal_comps) <- c("Validated representative sample", "June '20 Sample", "Full PA panel")
rownames(internal_comps) <- c("Proportion female", "Mean age", "Mean years of education",
                              "Mean income", "Proportion Democrat", "Proportion Republican", "N")

xtable(internal_comps, type = "latex")

# End Table 9 ####

# Table 10 ####

df1 <- dta.m %>%
  filter(., sen2022_dem != "I won’t vote in the Democratic primary")
sen2022_dems_dist <- count(x = df1, sen2022_dem, wt = weight.W13)
sen2022_dems_dist$freq <- round((sen2022_dems_dist$n / sum(sen2022_dems_dist$n)) * 100,1) 
sen2022_dems_dist

df1 <-  dta.m %>%
  filter(., sen2022_gop != "I won't vote in the Republican primary")
sen2022_gop_dist <- count(x = df1, sen2022_gop, wt = weight.W13)
sen2022_gop_dist$freq <- round((sen2022_gop_dist$n / sum(sen2022_gop_dist$n)) * 100,1) 
sen2022_gop_dist

df1 <-  dta.m %>%
  filter(., gov2022_gop != "I won’t vote in the Republican primary")
gov2022_gop_dist <- count(x = df1, gov2022_gop, wt = weight.W13)
gov2022_gop_dist$freq <- round((gov2022_gop_dist$n / sum(gov2022_gop_dist$n)) * 100,1) 
gov2022_gop_dist
## Careful when running these chunks, ' vs. ’ can cause trouble

sen2022_dems_dist$result <- NA
sen2022_gop_dist$result <- NA
gov2022_gop_dist$result <- NA

## source: https://www.electionreturns.pa.gov/_ENR/Home/SummaryResults?ElectionID=94&ElectionType=P&IsActive=0

sen2022_dems_dist$sen2022_dem
dem_results <- c(26.27, 58.65, 10.85, 4.24, NA)
sen2022_gop_dist
gopsen_results <- c(5.45, 31.14, 4.95, 24.66, 31.21, 1.51, 1.08, NA)
gov2022_gop_dist
gopgov_results <- c(15.78, 1.33, 9.56, 43.82, 1.93, 2.07, 20.25, 4.06, 1.2, NA)

sen2022_dems_dist$result <- dem_results
sen2022_gop_dist$result <- gopsen_results
gov2022_gop_dist$result <- gopgov_results

sen2022_dems_dist <- arrange(sen2022_dems_dist, -result)
sen2022_gop_dist <- arrange(sen2022_gop_dist, -result)
gov2022_gop_dist <- arrange(gov2022_gop_dist, -result)

sen2022_dems_dist <- rename(sen2022_dems_dist, candidate = sen2022_dem)
sen2022_gop_dist <- rename(sen2022_gop_dist, candidate = sen2022_gop)
gov2022_gop_dist <- rename(gov2022_gop_dist, candidate = gov2022_gop)

all2022_dist <- rbind.data.frame(sen2022_dems_dist, sen2022_gop_dist, gov2022_gop_dist)
# library(xtable)

all2022_dist <- all2022_dist[,-2]
primary_race <- c("Democratic Senate", rep(NA,4), "Republican Senate", rep(NA,7),
                  "Republican Governor", rep(NA,9))
all2022_dist <- cbind(primary_race, all2022_dist)

colnames(all2022_dist) <- c("Primary Race", "Candidate", "PA panel est.", "Result")
xtable(all2022_dist)

# End Table 10 ####


# Table 11 ####
##

vars <- c("DEMOCRAT","REPUBLICAN","TRUMPVBIDEN.ALL","AGE","BLACK","HISP","WHITE","FEMALE","EDYEARS")
rmat <- matrix(NA,length(vars),4)
for(i in 1:length(vars)){
  txt <- paste("tout <- t.test(dta.m$",vars[i],"[dta.m$civiqs_id %in% civiqs_id$pre.target],",
               "dta.m$",vars[i],"[dta.m$civiqs_id %in% civiqs_id$post.target])",sep="")
  eval(parse(text=txt))
  rmat[i,1] <- tout$estimate[1]
  rmat[i,2] <- tout$estimate[2]
  rmat[i,3] <- tout$p.value
  
  txt2 <- paste("hold <- dta.m$",vars[i],"[dta.m$civiqs_id %in% c(civiqs_id$pre.target,civiqs_id$post.target)]",sep="")
  eval(parse(text=txt2))
  
  rmat[i,4] <- length(na.omit(hold))
}
rownames(rmat) <- vars
colnames(rmat) <- c("Mean Before", "Mean After", "P-Value", "Total N")

xtable(rmat,digits=c(0,3,3,3,0))

# End Table 11 ####